package com.summer.filesync.model;

import java.io.Serializable;
import java.util.Objects;

/** 文件元数据信息 存储文件的基本信息，用于同步判断 */
public class FileMetadata implements Serializable, Comparable<FileMetadata> {
  private static final long serialVersionUID = 1L;

  private Long id; // 数据库主键
  private String filePath; // 文件路径
  private long fileSize; // 文件大小
  private long lastModified; // 最后修改时间
  private String fileHash; // 文件哈希值(SHA-256)
  private String checksum; // 文件校验和(MD5)
  private String nodeId; // 所属节点ID
  private long createdAt; // 数据库创建时间
  private long updatedAt; // 数据库更新时间
  private boolean isDirectory; // 是否为目录
  private FileStatus status; // 文件状态

  public enum FileStatus {
    NORMAL, // 正常状态
    SYNCING, // 同步中
    CONFLICT, // 冲突状态
    DELETED // 已删除
  }

  public FileMetadata() {
    long now = System.currentTimeMillis();
    this.createdAt = now;
    this.updatedAt = now;
    this.status = FileStatus.NORMAL;
  }

  public FileMetadata(
      String filePath, long fileSize, long lastModified, String checksum, String nodeId) {
    this();
    this.filePath = filePath;
    this.fileSize = fileSize;
    this.lastModified = lastModified;
    this.checksum = checksum;
    this.nodeId = nodeId;
  }

  @Override
  public int compareTo(FileMetadata other) {
    return Long.compare(this.lastModified, other.lastModified);
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null || getClass() != obj.getClass()) return false;
    FileMetadata that = (FileMetadata) obj;
    return Objects.equals(filePath, that.filePath) && Objects.equals(nodeId, that.nodeId);
  }

  @Override
  public int hashCode() {
    return Objects.hash(filePath, nodeId);
  }

  @Override
  public String toString() {
    return "FileMetadata{"
        + "filePath='"
        + filePath
        + '\''
        + ", fileSize="
        + fileSize
        + ", lastModified="
        + lastModified
        + ", checksum='"
        + checksum
        + '\''
        + ", nodeId='"
        + nodeId
        + '\''
        + ", status="
        + status
        + '}';
  }

  // Getters and Setters
  public String getFilePath() {
    return filePath;
  }

  public void setFilePath(String filePath) {
    this.filePath = filePath;
  }

  public long getFileSize() {
    return fileSize;
  }

  public void setFileSize(long fileSize) {
    this.fileSize = fileSize;
  }

  public long getLastModified() {
    return lastModified;
  }

  public void setLastModified(long lastModified) {
    this.lastModified = lastModified;
  }

  public String getChecksum() {
    return checksum;
  }

  public void setChecksum(String checksum) {
    this.checksum = checksum;
  }

  public String getNodeId() {
    return nodeId;
  }

  public void setNodeId(String nodeId) {
    this.nodeId = nodeId;
  }

  public long getCreatedAt() {
    return createdAt;
  }

  public void setCreatedAt(long createdAt) {
    this.createdAt = createdAt;
  }

  public long getUpdatedAt() {
    return updatedAt;
  }

  public void setUpdatedAt(long updatedAt) {
    this.updatedAt = updatedAt;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getFileHash() {
    return fileHash;
  }

  public void setFileHash(String fileHash) {
    this.fileHash = fileHash;
  }

  public boolean isDirectory() {
    return isDirectory;
  }

  public void setDirectory(boolean directory) {
    isDirectory = directory;
  }

  public FileStatus getStatus() {
    return status;
  }

  public void setStatus(FileStatus status) {
    this.status = status;
  }
}
